﻿using System;
using System.Configuration;

namespace AdverseEventReportSystem.Utils {
	public class ConfigHelper {
		#region 得到AppSettings中的配置字符串信息
		/// <summary>
		/// 得到AppSettings中的配置字符串信息
		/// </summary>
		/// <param name="key">AppSetting中关键字KEY</param>
		/// <returns>AppSettings中的配置字符串信息</returns>
		public static string GetConfigString(string key) {
			string AppStr = ConfigurationManager.AppSettings.Get(key);
			return AppStr;
		}
		#endregion
		#region 获取连接字符串
		/// <summary>
		/// 获取连接字符串
		/// </summary>
		/// <param name="key">Connection中name的值</param>
		/// <returns></returns>
		public static string GetConnectionString(string key) {
			string connStr = ConfigurationManager.ConnectionStrings[key].ToString();
			return connStr;
		}
		#endregion
		#region 获取AppSettings中的配置Bool信息
		/// <summary>
		/// 获取AppSettings中的配置Bool信息
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static bool GetConfigBool(string key) {
			bool result = false;
			string cfgVal = GetConfigString(key);
			if (null != cfgVal && string.Empty != cfgVal) {
				try {
					result = bool.Parse(cfgVal);
				}
				catch (FormatException) {
					// Ignore format exceptions.
				}
			}
			return result;
		}
		#endregion
		#region 获取AppSettings中的配置Decimal信息
		/// <summary>
		/// 获取AppSettings中的配置Decimal信息
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static decimal GetConfigDecimal(string key) {
			decimal result = 0;
			string cfgVal = GetConfigString(key);
			if (null != cfgVal && string.Empty != cfgVal) {
				try {
					result = decimal.Parse(cfgVal);
				}
				catch (FormatException) {
					// Ignore format exceptions.
				}
			}

			return result;
		}
		#endregion
		#region 获取AppSettings中的配置int信息
		/// <summary>
		/// 获取AppSettings中的配置int信息
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static int GetConfigInt(string key) {
			int result = 0;
			string cfgVal = GetConfigString(key);
			if (null != cfgVal && string.Empty != cfgVal) {
				try {
					result = int.Parse(cfgVal);
				}
				catch (FormatException) {
					// Ignore format exceptions.
				}
			}

			return result;
		}
		#endregion
	}
}
